<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="sky.domain.Flight" %>
<%@ page import="java.util.Date" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat formatter2 = new SimpleDateFormat("dd.MM.yyyy");
    SimpleDateFormat formatter3 = new SimpleDateFormat("HH:mm");
%>
<div id="sidebar">
    <ul>
        <li>
            <div id="search" >
                <form method="get" action="#">
                    <div>
                        <input type="text" name="s" id="search-text" value="" />
                        <input type="submit" id="search-submit" value="GO" />
                    </div>
                </form>
            </div>
            <div style="clear: both;">&nbsp;</div>
        </li>
        <li>
            <c:if test="${log != 'log'}">
                <c:choose>
                    <c:when test="${uzver == null}">
                        <form method="POST" action="/j_spring_security_check" class="form1" id="sidebarLogin">
                            <p><spring:message code="label.login" />:</p>
                            <p><input type="text" name="j_username"/></p>
                            <p><spring:message code="label.password" />:</p>
                            <p><input type="password" name="j_password" /></p>
                            <p><spring:message code="label.remember" />:
                                <input type="checkbox" name="j_spring_security_remember_me" /></p>
                            <p><input type="submit" value="Login" />
                                <input type="reset" value="Clear" /></p>
                        </form>
                    </c:when>
                    <c:otherwise>
                        <p>Hi, ${uzver.firstName} ${uzver.secondName}!</p>
                        <c:set var="i" value="${0}" scope="page" />
                        <c:forEach items="${uzver.tickets}" var="ticket">
                            <c:set var="i" value="${i+1}" scope="page" />
                        </c:forEach>
                        <c:choose>
                            <c:when test="i == 0">
                                <p>You are not registered on any flight</p>
                            </c:when>
                            <c:otherwise>
                                <p>You are registered on ${i} flight<c:if test="${i != 1}">s</c:if>:</p>
                                <div>
                                <p>
                                    <c:forEach items="${userFlightList}" var="flight">
                                        <c:set var="fl" value="${flight}" scope="request" />
                                        <%
                                            Date date = null;
                                            Date date1 = null;
                                            if (request.getAttribute("t") != null) {
                                                date = ((Flight)request.getAttribute("t")).getDepartureDate();
                                            }
                                            if (request.getAttribute("fl") != null) {
                                                date1 = ((Flight)request.getAttribute("fl")).getDepartureDate();
                                            }
                                            boolean group = false;
                                            if (date != null && date1 != null && formatter2.format(date).equals(formatter2.format(date1))) {
                                                group = true;
                                            }
                                        %>
                                        <% if (group) {%>
                                            <c:set var="t" value="${flight}" scope="request" />
                                            <%
                                                date = formatter.parse(((Flight)request.getAttribute("t")).getDepartureDate().toString());
                                            %>
                                            </p><p>
                                        <% } else { %>
                                            <c:set var="t" value="${flight}" scope="request" />
                                            <%
                                                date = formatter.parse(((Flight)request.getAttribute("t")).getDepartureDate().toString());
                                            %>
                                            <%= formatter2.format(date) %>:</p>
                                            <p>
                                        <% } %>
                                        <span style="display: inline-block">${flight.departureAirport.name}</span>
                                        <span style="display: inline-block">${flight.destinationAirport.name}</span>
                                        <span style="display: inline-block"><%= formatter3.format(date) %></span>
                                    </c:forEach>
                                </p>
                                </div>
                            </c:otherwise>
                        </c:choose>
                    </c:otherwise>
                </c:choose>
            </c:if>
        </li>
        <li>
            <hr style="color: #345E9B; width: 80%; height: 1px">
            <p>Your airport: ${currentAirport}</p>
            <c:set var="airport" value="Minsk" scope="request" />
            <c:forEach items="${SFlightList}" var="flight">
                <c:set var="flight" value="${flight}" scope="request" />
                <jsp:include page="sheduleFlightInfo.jsp" />
            </c:forEach>
            <a href="/file?currentAirport=${currentAirport}">To File</a>
        </li>
    </ul>
</div>